package jjn.round1;

/**
 * @author Jjn
 * @since 2023/4/30 21:47
 */
public class Offer47_MaxValueOfPresents {
    public int maxValue(int[][] grid) {
        int[][] paths = new int[grid.length][grid[0].length];
        paths[0][0] = grid[0][0];
        for (int i = 1; i < grid.length; i++) {
            paths[i][0] = paths[i - 1][0] + grid[i][0];
        }
        for (int i = 1; i < grid[0].length; i++) {
            paths[0][i] = paths[0][i - 1] + grid[0][i];
        }
        for (int i = 1; i < grid.length; i++) {
            for (int j = 1; j < grid[0].length; j++) {
                paths[i][j] = Math.max(paths[i - 1][j], paths[i][j - 1]) + grid[i][j];
            }
        }
        return paths[grid.length - 1][grid[0].length - 1];
    }
}
